package com.GatherFour;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pInfo;
import android.os.Bundle;
import android.widget.TextView;


public class WiFiDirectHost extends WifiDirectManager {
	
	
	boolean isEnteredLobby = false;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
			
		setContentView(R.layout.creatinghost);
		
		
		context = this.getApplicationContext();
		
	//	manager.createGroup(channel, null);
	}
	

	@Override
	public void updateThisDevice(WifiP2pDevice device) {
		// TODO Auto-generated method stub
		setDeviceStatus(device,Player.SERVER);

        device.primaryDeviceType = "Server";
        device.secondaryDeviceType = "Game Host";

        TextView view = (TextView) findViewById(R.id.hostNameID);
        view.setText(device.primaryDeviceType);
        view = (TextView) findViewById(R.id.hostStatusID);
        view.setText(device.isGroupOwner()?"True":"False");
        
	}

	@Override
	public void onConnectionInfoAvailable(WifiP2pInfo info) {
		// TODO Auto-generated method stub
		//clientList.add(info);
		TextView view = (TextView) findViewById(R.id.hostStatusID);
		view.setText(info.groupOwnerAddress.getHostAddress());
//		view = (TextView) findViewById(R.id.groupOwnerID);
		view.setText(info.isGroupOwner?"True":"False");


		if(info.isGroupOwner){
			groupOwnerSetup("host");
		}else{
			try {
				setupServerListener();
				Thread.sleep(500);
				
				//setupServerListener();
				connectGroupOwner(info.groupOwnerAddress,"host");
				
				} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/*
	public void connectClient(){
		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Socket socket = new Socket(clientList.get(0).groupOwnerAddress,8988);
					OutputStream out = socket.getOutputStream();
					
					String message = "Hello, I am the game host!";
					out.write(message.getBytes());
					//TextView view = (TextView)findViewById(R.id.serverStatusID);
					//view.setText("Message Sent.");
					out.close();
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
				
			}
			
		}).start();
		Intent myIntent = new Intent(this.getApplicationContext(),GameLobby.class);	
		Bundle bundle = new Bundle();
		bundle.putString("clientAddr", clientList.get(0).groupOwnerAddress.getHostAddress());
		bundle.putString("self", "host");
		myIntent.putExtras(bundle);
		startActivity(myIntent);
	}
	
*/	
	public void setupServerListener(){
		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					ServerSocket socket = new ServerSocket(8989);
					
					while(!isEnteredLobby){
						Socket client = socket.accept();
						
						final Scanner sc = new Scanner(client.getInputStream());
						if(sc.hasNextLine()){
							
							clientList.add(sc.nextLine());
							
							user.clientAddrs[ClientID-1] = client.getInetAddress().getHostAddress();
							ClientID++;
						}
					}
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}).start();
	}
	
	
}
